---
sidebar_position: 3
tags: [createPoolCluster, URI, SHA1, RDS, SSL, Socks]
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import { FAQ } from '@site/src/components/FAQ';
import { ExternalCodeEmbed } from '@site/src/components/ExternalCodeEmbed';

# createPoolCluster

:::info
For queries please see the [**Simple Queries**](/docs/examples/queries/simple-queries) and [**Prepared Statements**](/docs/examples/queries/prepared-statements) examples.
:::

## add(group, connectionUri)

> **add(group: string, connectionUri: string)**

<Tabs>
  <TabItem value='promise.js' default>

```js
import mysql from 'mysql2/promise';

try {
  // highlight-start
  const poolCluster = mysql.createPoolCluster();

  poolCluster.add('clusterA', 'mysql://root:password@localhost:3306/test');
  // poolCluster.add('clusterB', '...');

  const connection = await poolCluster.getConnection('clusterA');
  // highlight-end
  // ... some query

  // highlight-next-line
  connection.release();
} catch (err) {
  console.log(err);
}
```

  </TabItem>
  <TabItem value='callback.js'>

```js
const mysql = require('mysql2');

const poolCluster = mysql.createPoolCluster();

poolCluster.add('clusterA', 'mysql://root:password@localhost:3306/test');
// poolCluster.add('clusterB', '...');

poolCluster.getConnection('clusterA', function (err, connection) {
  if (err instanceof Error) {
    console.log(err);
    return;
  }

  // ... some query

  connection.release();
});
```

  </TabItem>
</Tabs>

:::warning

Don't forget to release the connection when finished by using:

- `connection.release()`

:::

<hr />

## add(group, config)

> **add(group: string, config: [PoolOptions](#pooloptions))**

<Tabs>
  <TabItem value='promise.js' default>

```js
import mysql from 'mysql2/promise';

try {
  // highlight-start
  const poolCluster = mysql.createPoolCluster();

  poolCluster.add('clusterA', {
    host: 'localhost',
    user: 'root',
    database: 'test',
    // port: 3306,
    // password: '',
  });
  // poolCluster.add('clusterB', '...');

  const connection = await poolCluster.getConnection('clusterA');
  // highlight-end
  // ... some query

  // highlight-next-line
  connection.release();
} catch (err) {
  console.log(err);
}
```

  </TabItem>
  <TabItem value='callback.js'>

```js
const mysql = require('mysql2');

const poolCluster = mysql.createPoolCluster();

poolCluster.add('clusterA', {
  host: 'localhost',
  user: 'root',
  database: 'test',
  // port: 3306,
  // password: '',
});
// poolCluster.add('clusterB', '...');

poolCluster.getConnection('clusterA', function (err, connection) {
  if (err instanceof Error) {
    console.log(err);
    return;
  }

  // ... some query

  connection.release();
});
```

  </TabItem>
</Tabs>

:::warning

Don't forget to release the connection when finished by using:

- `connection.release()`

:::

<hr />

## add(group, config) — SHA1

> **add(group: string, config: [PoolOptions](#pooloptions))**

<Tabs>
  <TabItem value='promise.js' default>

```js
import mysql from 'mysql2/promise';

try {
  // highlight-start
  const poolCluster = mysql.createPoolCluster();

  poolCluster.add('clusterA', {
    // ...
    passwordSha1: Buffer.from(
      '8bb6118f8fd6935ad0876a3be34a717d32708ffd',
      'hex'
    ),
  });
  // poolCluster.add('clusterB', '...');

  const connection = await poolCluster.getConnection('clusterA');
  // highlight-end
  // ... some query

  // highlight-next-line
  connection.release();
} catch (err) {
  console.log(err);
}
```

  </TabItem>
  <TabItem value='callback.js'>

```js
const mysql = require('mysql2');

const poolCluster = mysql.createPoolCluster();

poolCluster.add('clusterA', {
  // ...
  passwordSha1: Buffer.from('8bb6118f8fd6935ad0876a3be34a717d32708ffd', 'hex'),
});
// poolCluster.add('clusterB', '...');

poolCluster.getConnection('clusterA', function (err, connection) {
  if (err instanceof Error) {
    console.log(err);
    return;
  }

  // ... some query

  connection.release();
});
```

  </TabItem>
</Tabs>

:::warning

Don't forget to release the connection when finished by using:

- `connection.release()`

:::

<hr />

## add(group, config) — SSL

> **add(group: string, config: [PoolOptions](#pooloptions))**

<Tabs>
  <TabItem value='promise.js' default>

```js
import mysql from 'mysql2/promise';

try {
  // highlight-start
  const poolCluster = mysql.createPoolCluster();

  poolCluster.add('clusterA', {
    // ...
    ssl: {
      // key: fs.readFileSync('./certs/client-key.pem'),
      // cert: fs.readFileSync('./certs/client-cert.pem')
      ca: fs.readFileSync('./certs/ca-cert.pem'),
    },
  });
  // poolCluster.add('clusterB', '...');

  const connection = await poolCluster.getConnection('clusterA');
  // highlight-end
  // ... some query

  // highlight-next-line
  connection.release();
} catch (err) {
  console.log(err);
}
```

  </TabItem>
  <TabItem value='callback.js'>

```js
const mysql = require('mysql2');

const poolCluster = mysql.createPoolCluster();

poolCluster.add('clusterA', {
  // ...
  ssl: {
    // key: fs.readFileSync('./certs/client-key.pem'),
    // cert: fs.readFileSync('./certs/client-cert.pem')
    ca: fs.readFileSync('./certs/ca-cert.pem'),
  },
});
// poolCluster.add('clusterB', '...');

poolCluster.getConnection('clusterA', function (err, connection) {
  if (err instanceof Error) {
    console.log(err);
    return;
  }

  // ... some query

  connection.release();
});
```

  </TabItem>
    <TabItem value='certs/ca-cert.pem'>
    <ExternalCodeEmbed
      language='plan'
      url='https://raw.githubusercontent.com/sidorares/node-mysql2/master/test/fixtures/ssl/certs/ca.pem'
    />

    - See [ssl/certs](https://github.com/sidorares/node-mysql2/tree/master/test/fixtures/ssl/certs).

  </TabItem>
</Tabs>

:::warning

Don't forget to release the connection when finished by using:

- `connection.release()`

:::

<hr />

## add(group, config) — RDS SSL

> **add(group: string, config: [PoolOptions](#pooloptions))**

You can use **Amazon RDS** string as value to ssl property to connect to **Amazon RDS** MySQL over SSL.

In that case https://s3.amazonaws.com/rds-downloads/mysql-ssl-ca-cert.pem CA cert is used:

```sh
npm install --save aws-ssl-profiles
```

<Tabs>
  <TabItem value='promise.js' default>

```js
import mysql from 'mysql2/promise';
import awsCaBundle from 'aws-ssl-profiles';

try {
  // highlight-start
  const poolCluster = mysql.createPoolCluster();

  poolCluster.add('clusterA', {
    // ...
    host: 'db.id.ap-southeast-2.rds.amazonaws.com',
    ssl: awsCaBundle,
  });
  // poolCluster.add('clusterB', '...');

  const connection = await poolCluster.getConnection('clusterA');
  // highlight-end
  // ... some query

  // highlight-next-line
  connection.release();
} catch (err) {
  console.log(err);
}
```

:::info
For detailed instructions, please follow the [**AWS SSL Profiles documentation**](https://github.com/mysqljs/aws-ssl-profiles?tab=readme-ov-file#readme).
:::

:::tip Testing

```js
try {
  const [res] = await connection.query('SHOW `status` LIKE "Ssl_cipher"');
  await poolCluster.end();

  console.log(res);
} catch (err) {
  console.log(err);
}
```

:::

  </TabItem>
  <TabItem value='callback.js'>

```js
const mysql = require('mysql2');
const awsCaBundle = require('aws-ssl-profiles');

const poolCluster = mysql.createPoolCluster();

poolCluster.add('clusterA', {
  // ...
  host: 'db.id.ap-southeast-2.rds.amazonaws.com',
  ssl: awsCaBundle,
});
// poolCluster.add('clusterB', '...');

poolCluster.getConnection('clusterA', function (err, connection) {
  if (err instanceof Error) {
    console.log(err);
    return;
  }

  // ... some query

  connection.release();
});
```

:::info
For detailed instructions, please follow the [**AWS SSL Profiles documentation**](https://github.com/mysqljs/aws-ssl-profiles?tab=readme-ov-file#readme).
:::

:::tip Testing

```js
connectionquery('SHOW `status` LIKE "Ssl_cipher"', function (err, res) {
  poolCluster.end();

  if (err instanceof Error) {
    console.log(err);
    return;
  }

  console.log(res);
});
```

:::

  </TabItem>
</Tabs>

:::warning

Don't forget to release the connection when finished by using:

- `connection.release()`

:::

<hr />

## add(group, config) — Socks

> **add(group: string, config: [PoolOptions](#pooloptions))**

<Tabs>
  <TabItem value='A.js'>

```js
const mysql = require('mysql2');
const SocksConnection = require('socksjs');

const socksProxy = new SocksConnection({ port: 3306 });
// highlight-start
const poolCluster = mysql.createPoolCluster();

poolCluster.add('clusterA', {
  stream: socksProxy,
});
// poolCluster.add('clusterB', '...');

const poolNamespace = poolCluster.of('clusterA');
// highlight-end
```

  </TabItem>
  <TabItem value='B.js'>

```js
const mysql = require('mysql2');
const SocksConnection = require('socksjs');

// highlight-start
const poolCluster = mysql.createPoolCluster();

poolCluster.add('clusterA', {
  debug: 1,
  stream: function () {
    return new SocksConnection({ port: 3306 });
  },
});
// poolCluster.add('clusterB', '...');

const poolNamespace = poolCluster.of('clusterA');
// highlight-end
```

  </TabItem>
</Tabs>

:::tip Testing

```js
poolNamespace.execute('SELECT SLEEP(1.1) AS `www`', (err, rows, fields) => {
  if (err instanceof Error) {
    console.log(err);
    return;
  }

  console.log(rows, fields);
});

poolNamespace.execute('SELECT SLEEP(1) AS `qqq`', (err, rows, fields) => {
  if (err instanceof Error) {
    console.log(err);
    return;
  }

  console.log(rows, fields);
});

poolNamespace.execute('SELECT SLEEP(1) AS `qqq`', (err, rows, fields) => {
  if (err instanceof Error) {
    console.log(err);
    return;
  }

  console.log(rows, fields);
});
```

:::

<hr />

## Glossary

### PoolOptions

<blockquote>
  **PoolOptions** extends all options from **ConnectionOptions**:

  <FAQ title='ConnectionOptions Specification'>
    <ExternalCodeEmbed
      language='ts'
      url='https://raw.githubusercontent.com/sidorares/node-mysql2/master/typings/mysql/lib/Connection.d.ts'
      extractMethod='ConnectionOptions'
      methodType='interface'
    />
  </FAQ>
</blockquote>

<FAQ title='PoolOptions Specification'>
  <ExternalCodeEmbed
    language='ts'
    url='https://raw.githubusercontent.com/sidorares/node-mysql2/master/typings/mysql/lib/Pool.d.ts'
    extractMethod='PoolOptions'
    methodType='interface'
  />
</FAQ>
